package com.dating.core.auth;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.util.ByteSource;
import org.nutz.lang.Lang;

public class AuthCredentialsMatcher extends SimpleCredentialsMatcher  {

	@Override
	public boolean doCredentialsMatch(AuthenticationToken token,
			AuthenticationInfo info) {
		UsernamePasswordToken authToken = (UsernamePasswordToken) token;
		return Lang.md5(String.valueOf(authToken.getPassword()) + getSalt(info)).equals(info.getCredentials());
	}
	
	
	
	
	/**
	 * 获取密码盐值
	 * @param info
	 * @return
	 */
	private ByteSource getSalt(AuthenticationInfo info){
		ByteSource salt = null;
		if(info instanceof SimpleAuthenticationInfo){
			salt = ((SimpleAuthenticationInfo)info).getCredentialsSalt();
		}
		if(salt == null){
			salt = ByteSource.Util.bytes(info.getPrincipals());
		}
		return salt ;
	}
	
}
